package com.jichaoyun.task;

import com.jichaoyun.service.IFlowDayService;
import com.jichaoyun.service.IFlowHourService;
import com.jichaoyun.service.IFlowMonthService;
import com.jichaoyun.service.IStationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.annotation.PostConstruct;
import java.io.File;
import java.net.FileNameMap;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {

    @Autowired
    private IFlowHourService flowHourService;

    @Autowired
    private IFlowDayService flowDayService;

    @Autowired
    private IFlowMonthService flowMonthService;

    @Autowired
    private IStationInfoService stationInfoService;


    private List<Integer> stationNoList;

    // 在依赖注入完成后初始化 stationNoList
    @PostConstruct
    public void init() {
        this.stationNoList = stationInfoService.getStationNoList();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 每日定时生成日报表
        for (int stationNo : stationNoList) {
            int accountHour = stationInfoService.getAccountHourByStation(stationNo);
            String cronExpression = String.format("0 0 %d * * ?", accountHour);

            taskRegistrar.addCronTask(() -> {
                try {
                    // 记录日志
                    // System.out.println("定时任务执行生成日报表：站点编号: " + stationNo + ", Cron expression: " + cronExpression);
                    // 获取当前日期
                    LocalDate today = LocalDate.now();
                    String dateStr = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                    // 调用生成报表的方法
                    File excelFile = flowHourService.generateDailyExcelFile(stationNo, dateStr);

                    // 获取站点名称
                    String stationName = stationInfoService.getStationNameByStationNo(stationNo);

                    // 文件名称
                    String fileName = dateStr + "_" + stationName +  "_日报导出表.xlsx";
                    String filePath = "C:/Users/Administrator/Desktop/dayExcel/"  + fileName;
                    // 将文件保存到指定路径或上传到云存储
                    saveOrUploadFile(excelFile, filePath);

                } catch (Exception e) {
                    // 记录日志
                    e.printStackTrace();
                }
            }, cronExpression);
        }

        // 每月定时生成月报表
        for (int stationNo : stationNoList) {
            int accountDate = stationInfoService.getAccountDateByStation(stationNo);
            String cronExpression = String.format("59 59 23 %d * ?", accountDate);
//            int accountDate = 8;
//            String cronExpression = String.format("0 28 20 %d * ?", accountDate);
            taskRegistrar.addCronTask(() -> {
                try {
                    // 记录日志
                    System.out.println("定时任务执行生成月报表：站点编号: " + stationNo + ", Cron expression: " + cronExpression);
                    // 获取当前日期
                    LocalDate today = LocalDate.now();
                    int year = today.getYear();
                    int month = today.getMonthValue();

                    // 调用生成报表的方法
                    File excelFile = flowDayService.generateMonthlyExcelFile(stationNo, year,month);
                    // 保存路径
                    String stationName = stationInfoService.getStationNameByStationNo(stationNo);
                    String yearStr = String.valueOf(year);
                    String monthStr = String.valueOf(month);
                    String fileName = stationName +  "_" + yearStr +"年" + monthStr +"月报导出表.xlsx";
                    String filePath = "C:/Users/Administrator/Desktop/monthExcel/"  + fileName;
                    saveOrUploadFile(excelFile, filePath);

                } catch (Exception e) {
                    // 记录日志
                    e.printStackTrace();
                }
            }, cronExpression);
        }

        // 每年定时生成年报表
        for (int stationNo : stationNoList) {
            String cronExpression = "59 59 23 31 12 ?";
//            String cronExpression = "0 43 8 9 4 ?";
            taskRegistrar.addCronTask(() -> {
                try {
                    // 记录日志
                    System.out.println("定时任务执行年报表自动生成：站点编号: " + stationNo + ", Cron expression: " + cronExpression);
                    // 获取当前日期
                    LocalDate today = LocalDate.now();
                    int year = today.getYear();
                    // 调用生成报表的方法
                    File excelFile = flowMonthService.generateYearlyExcelFile(stationNo, year);

                    // 文件保存路径
                    String stationName = stationInfoService.getStationNameByStationNo(stationNo);
                    String yearStr = String.valueOf(year);
                    String fileName = stationName +  "_" + yearStr +"年报导出表.xlsx";
                    String filePath = "C:/Users/Administrator/Desktop/yearExcel/"  + fileName;

                    // 将文件保存到指定路径或上传到云存储
                    saveOrUploadFile(excelFile, filePath);

                } catch (Exception e) {
                    // 记录日志
                    e.printStackTrace();
                }
            }, cronExpression);
        }

}

    private void saveOrUploadFile(File file, String filePath) {
        try {
            java.nio.file.Files.move(file.toPath(), java.nio.file.Paths.get(filePath), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}